/*  File Name	: EnginePlayer.h
 *  Author		: Javie Pong
 *	 E-mail		: 823960168@qq.com
 *  Created on	: 2012-7-1
 *  Description:
 */

#ifndef ENGINEPLAYER_H_
#define ENGINEPLAYER_H_

#include "Lock.h"
#include "MessageBlock.h"
#include "Poker.h"
#include "Cmd.h"
#include "CommonStruct.h"

class EngineMonitor;
class Table;
class MySqlDB;

enum GameStatus
{
	GAME_STATUS_STOP=0,//未开始游戏
	GAME_STATUS_PREPARE=1,//准备游戏
	GAME_STATUS_START = 2,//正在游戏
	GAME_STATUS_ALL_IN =3,//全下
	GAME_STATUS_FOLD =4//弃牌
};

struct Operation
{
	Operation()
	{
		reset();
	}
	void reset();
	bool bring_chips;
	bool start;
	bool bet;
	bool raise;
	bool call;
	bool check;
	bool all_in;
	bool fold;
};

class EnginePlayer:public PlayerBaseInfo
{
	friend class Table;
	friend class MySqlDB;
public:
	EnginePlayer(int p=0);
	virtual ~EnginePlayer();

	EngineMonitor *monitor(EngineMonitor *monitor=NULL);

	int enter_room(int gate,MessageBlock *mb);
	int exit_room(int cmd,MessageBlock *mb=NULL);

	int enter_table(MessageBlock *mb);
	int exit_table(MessageBlock *mb=NULL,bool notify=true);

	int start_game(MessageBlock *mb);
	int bet(MessageBlock *mb);//下注
	int raise(MessageBlock *mb);//加赌注
	int all_in(MessageBlock *mb=NULL);//全下
	int fold(MessageBlock *mb=NULL,bool force=false);//弃牌
	int call(MessageBlock *mb);//跟牌
	int check(MessageBlock *mb);//过牌

	int modify_chips(MessageBlock *mb=NULL);
	int notify_operation(int max_bet);

	bool is_all_in(void);
	bool is_fold(void);
	bool is_in_game(void);//是否参与了游戏筹码分配
	bool is_gaming(void);//是否正在游戏
	bool is_ready(void);//是否已准备开始游戏

	int notify_player_list(void);
	int notify_update_info(int type,MessageBlock *mb=NULL);
	int broadcast_operation(int type,int num=0);

	int respond_success_to_client(int cmd,MessageBlock *mb=NULL);
	int respond_error_to_client(int cmd,int err,MessageBlock *mb=NULL);

	//const PlayerBaseInfo &base_info(void);
	int room_id(void);
	int gate_id(void) const;
	int table_id(void) const;
	int table_index(void) const;
	void table_index(int index);
	int seat(void);

	void reset(void);
	void exit_table_reset(void);
	void stop_game_reset(void);
	void set_game_status(GameStatus status);
	void sub_cost(RecordChips type,int cost);
	void chips_serial_record(RecordChips type,int amount);
	void save_data(void);
	void game_end(void);

	const DBInfo &dbinfo(void);
	MySqlDB *globaldb(void);

	int total_chips(void);
private:
	//PlayerBaseInfo base_info_;
	int gate_id_;

	EngineMonitor *monitor_;
	GameStatus game_status_;
	GameStep game_step_;

	bool auto_bring_in_;
	int last_bring_chips_;
	int cur_bet_chips_;
	int chips_in_table_;
	int cur_use_chips_;
	int win_chips_;
	int table_index_;
	Table *cur_table_;

	Card* card_[2];
	bool is_wait_;
	bool notify_bring_chips_;
	bool notify_start_;
	RT_MUTEX rt_mutex_;
	Operation operation_;

	DBInfo db_info_;
};

#endif /* ENGINEPLAYER_H_ */
